package routes

import (
	"go-weiqi-system/config"
	"go-weiqi-system/internal/controllers/admin"
	"go-weiqi-system/internal/controllers/api"
	"go-weiqi-system/internal/middleware"

	"github.com/gin-gonic/gin"
)

func InitRoutes(r *gin.Engine, cfg *config.Config) {
	// 添加CORS中间件
	r.Use(middleware.CORS())

	// 初始化控制器
	// 管理后台控制器
	adminLogin := &admin.LoginController{Config: cfg}
	adminBanner := &admin.BannerController{}
	adminTeacher := &admin.TeacherController{}
	adminCourse := &admin.CourseController{}
	adminCompetition := &admin.CompetitionController{}
	adminActivity := &admin.ActivityController{}
	adminFAQ := &admin.FAQController{}
	adminAppointmentTime := &admin.AppointmentTimeController{}
	adminAppointment := &admin.AppointmentController{}
	adminCompetitionReg := &admin.CompetitionRegistrationController{}
	adminUser := &admin.UserController{}
	adminSuggestion := &admin.SuggestionController{}
	adminCourseLevel := &admin.CourseLevelController{}
	adminOrder := &admin.OrderController{}
	adminUpload := &admin.UploadController{Config: cfg}

	// 用户端控制器
	apiAuth := &api.AuthController{Config: cfg}
	apiIndex := &api.IndexController{}
	apiCourse := &api.CourseController{}
	apiCompetition := &api.CompetitionController{}
	apiCompetitionReg := &api.CompetitionRegistrationController{}
	apiActivity := &api.ActivityController{}
	apiFAQ := &api.FAQController{}
	apiTeacher := &api.TeacherController{}
	apiCourseLevel := &api.CourseLevelController{}
	apiAppointment := &api.AppointmentController{}
	apiRanking := &api.RankingController{}
	apiSuggestion := &api.SuggestionController{}
	apiOrder := &api.OrderController{}
	apiMyCourse := &api.MyCourseController{}
	apiNotification := &api.NotificationController{}

	// 管理后台路由组
	adminGroup := r.Group("/admin")
	{
		// 不需要认证的路由
		adminGroup.POST("/login", adminLogin.Login)

		// 需要认证的路由
		authGroup := adminGroup.Group("")
		authGroup.Use(middleware.AuthAdmin(cfg))
		{
			// 管理员信息
			authGroup.GET("/info", adminLogin.GetAdminInfo)

			// 轮播图管理
			authGroup.GET("/banner/index", adminBanner.Index)
			authGroup.POST("/banner/add", adminBanner.Add)
			authGroup.POST("/banner/edit", adminBanner.Edit)
			authGroup.POST("/banner/delete", adminBanner.Delete)

			// 教师管理
			authGroup.GET("/teacher/index", adminTeacher.Index)
			authGroup.POST("/teacher/add", adminTeacher.Add)
			authGroup.POST("/teacher/edit", adminTeacher.Edit)
			authGroup.POST("/teacher/delete", adminTeacher.Delete)

			// 课程管理
			authGroup.GET("/course/index", adminCourse.Index)
			authGroup.POST("/course/add", adminCourse.Add)
			authGroup.POST("/course/edit", adminCourse.Edit)
			authGroup.POST("/course/delete", adminCourse.Delete)

			// 比赛管理
			authGroup.GET("/competition/index", adminCompetition.Index)
			authGroup.POST("/competition/add", adminCompetition.Add)
			authGroup.POST("/competition/edit", adminCompetition.Edit)
			authGroup.POST("/competition/delete", adminCompetition.Delete)

			// 活动管理
			authGroup.GET("/activity/index", adminActivity.Index)
			authGroup.POST("/activity/add", adminActivity.Add)
			authGroup.POST("/activity/edit", adminActivity.Edit)
			authGroup.POST("/activity/delete", adminActivity.Delete)

			// FAQ管理
			authGroup.GET("/faq/index", adminFAQ.Index)
			authGroup.POST("/faq/add", adminFAQ.Add)
			authGroup.POST("/faq/edit", adminFAQ.Edit)
			authGroup.POST("/faq/delete", adminFAQ.Delete)

			// 约棋时间段管理
			authGroup.GET("/appointment-time/index", adminAppointmentTime.Index)
			authGroup.POST("/appointment-time/add", adminAppointmentTime.Add)
			authGroup.POST("/appointment-time/edit", adminAppointmentTime.Edit)
			authGroup.POST("/appointment-time/delete", adminAppointmentTime.Delete)

			// 约棋报名管理
			authGroup.GET("/appointment/index", adminAppointment.Index)
			authGroup.POST("/appointment/update-status", adminAppointment.UpdateStatus)
			authGroup.POST("/appointment/batch-check-in", adminAppointment.BatchCheckIn)
			authGroup.GET("/appointment/statistics", adminAppointment.Statistics)
			authGroup.GET("/appointment/export", adminAppointment.Export)

			// 比赛报名管理
			authGroup.GET("/competition-registration/index", adminCompetitionReg.Index)
			authGroup.GET("/competition-registration/detail", adminCompetitionReg.Detail)
			authGroup.POST("/competition-registration/update-status", adminCompetitionReg.UpdateStatus)
			authGroup.GET("/competition-registration/statistics", adminCompetitionReg.Statistics)
			authGroup.GET("/competition-registration/export", adminCompetitionReg.Export)

			// 用户管理
			authGroup.GET("/user/index", adminUser.Index)
			authGroup.GET("/user/detail", adminUser.Detail)
			authGroup.POST("/user/update-status", adminUser.UpdateStatus)
			authGroup.POST("/user/update-score", adminUser.UpdateScore)
			authGroup.GET("/user/statistics", adminUser.Statistics)

			// 建议管理
			authGroup.GET("/suggestion/index", adminSuggestion.Index)
			authGroup.GET("/suggestion/detail", adminSuggestion.Detail)
			authGroup.POST("/suggestion/reply", adminSuggestion.Reply)
			authGroup.POST("/suggestion/update-status", adminSuggestion.UpdateStatus)
			authGroup.POST("/suggestion/delete", adminSuggestion.Delete)
			authGroup.GET("/suggestion/statistics", adminSuggestion.Statistics)

			// 课程等级管理
			authGroup.GET("/course-level/index", adminCourseLevel.Index)
			authGroup.POST("/course-level/add", adminCourseLevel.Add)
			authGroup.POST("/course-level/edit", adminCourseLevel.Edit)
			authGroup.POST("/course-level/delete", adminCourseLevel.Delete)

			// 订单管理
			authGroup.GET("/order/index", adminOrder.Index)
			authGroup.GET("/order/detail", adminOrder.Detail)
			authGroup.GET("/order/statistics", adminOrder.Statistics)

			// 文件上传
			authGroup.POST("/upload", adminUpload.Upload)
		}
	}

	// 用户端路由组
	apiGroup := r.Group("/api")
	{
		// 不需要认证的路由
		// 登录注册
		apiGroup.POST("/auth/register", apiAuth.Register)
		apiGroup.POST("/auth/login", apiAuth.Login)
		apiGroup.POST("/auth/wx-login", apiAuth.WxLogin)

		// 首页
		apiGroup.GET("/index", apiIndex.Index)
		apiGroup.GET("/banner/list", apiIndex.BannerList)

		// 课程
		apiGroup.GET("/course/index", apiCourse.Index)
		apiGroup.GET("/course/detail", apiCourse.Detail)

		// 比赛
		apiGroup.GET("/competition/index", apiCompetition.Index)
		apiGroup.GET("/competition/detail", apiCompetition.Detail)

		// 活动
		apiGroup.GET("/activity/index", apiActivity.Index)
		apiGroup.GET("/activity/detail", apiActivity.Detail)
		apiGroup.GET("/activity/categories", apiActivity.CategoryList)

		// FAQ
		apiGroup.GET("/faq/index", apiFAQ.Index)

		// 教师
		apiGroup.GET("/teacher/index", apiTeacher.Index)
		apiGroup.GET("/teacher/detail", apiTeacher.Detail)

		// 课程等级
		apiGroup.GET("/course-level/index", apiCourseLevel.Index)

		// 约棋时间段
		apiGroup.GET("/appointment/timeslots", apiAppointment.TimeSlots)
		apiGroup.GET("/appointment/recommend", apiAppointment.Recommend)
		apiGroup.GET("/appointment/index", apiAppointment.Index)

		// 排行榜
		apiGroup.GET("/ranking/level", apiRanking.LevelRanking)
		apiGroup.GET("/ranking/contribution", apiRanking.ContributionRanking)
		apiGroup.GET("/ranking/winrate", apiRanking.WinRateRanking)

		// 需要认证的路由
		authGroup := apiGroup.Group("")
		authGroup.Use(middleware.AuthUser(cfg))
		{
			// 用户信息
			authGroup.GET("/auth/user-info", apiAuth.GetUserInfo)
			authGroup.POST("/auth/update-info", apiAuth.UpdateUserInfo)
			authGroup.POST("/auth/bind-phone", apiAuth.BindPhone)
			authGroup.POST("/auth/decrypt-phone", apiAuth.DecryptPhone)

			// 约棋
			authGroup.GET("/appointment/my", apiAppointment.MyList)
			authGroup.POST("/appointment/create", apiAppointment.Create)
			authGroup.POST("/appointment/cancel", apiAppointment.Cancel)

			// 比赛报名
			authGroup.POST("/competition/register", apiCompetitionReg.Register)
			authGroup.GET("/competition/my-registrations", apiCompetitionReg.MyList)
			authGroup.GET("/competition/check-registrations", apiCompetitionReg.CheckMyRegistrations)
			authGroup.GET("/competition/registration-detail", apiCompetitionReg.Detail)
			authGroup.POST("/competition/cancel-registration", apiCompetitionReg.Cancel)

			// 建议
			authGroup.POST("/suggestion/submit", apiSuggestion.Submit)
			authGroup.GET("/suggestion/my", apiSuggestion.MyList)

			// 订单
			authGroup.POST("/order/create", apiOrder.CreateOrder)
			authGroup.GET("/order/my", apiOrder.MyOrders)
			authGroup.GET("/order/detail", apiOrder.OrderDetail)
			authGroup.POST("/order/cancel", apiOrder.CancelOrder)
			authGroup.POST("/order/mock-pay", apiOrder.MockPay)

			// 我的课程
			authGroup.GET("/my-course/index", apiMyCourse.Index)
			authGroup.GET("/my-course/check", apiMyCourse.CheckPurchased)
			authGroup.GET("/my-course/detail", apiMyCourse.Detail)

			// 通知
			authGroup.GET("/notification/index", apiNotification.Index)
			authGroup.GET("/notification/detail", apiNotification.Detail)
			authGroup.POST("/notification/mark-read", apiNotification.MarkRead)
			authGroup.POST("/notification/mark-all-read", apiNotification.MarkAllRead)
			authGroup.GET("/notification/unread-count", apiNotification.UnreadCount)
			authGroup.POST("/notification/delete", apiNotification.Delete)
		}
	}
}
